package top.zxybird.demo.juc;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by zxybird on 2018/2/22.
 */
public class AtomicIntegerDemo {

  // AtomicInteger底层基于CAS元语
  private static AtomicInteger count = new AtomicInteger(0);

  private static Integer count2 = 0;

  private static int addCnt = 100;

  public static void main(String[] args) throws InterruptedException {

    Runnable addCount = () -> {
      for (int i = 1; i <= addCnt; i++) {
        count.incrementAndGet();
        count2++;
      }

    };

    int threadLen = 100;

    List<Thread> threadList = new ArrayList<>(threadLen);

    for (int i = 1; i <= threadLen; i++) {
      Thread thread = new Thread(addCount);
      thread.start();
      threadList.add(thread);
    }

    for (Thread thread : threadList) {
      thread.join();
    }

    System.out.println(count.get());
    System.out.println(count2);

  }

}
